package ru.spesit.ads.mailru 
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	import flash.utils.*;
	
	/**
	 * Class WideBanner
	 * Контейнер для широкого баннера, обчное местоположение внизу приложения
	 * размеры загружаемого баненра 560х90, размер контейнера 570х100
	 * 
	 * @author Yuriy (Mur4ik) Kulkoff
	 * @version 0.1
	 * @date 14.06.2010
	 */
	public class WideBanner extends Sprite
	{
		private var _userData:XML;
		private var _bannerURL:String;
		private var _bannerLink:String;
		private var _refreshInterval:Number;
		private var _urlRequest:URLRequest;
		private var _urlLoader:URLLoader;
		
		private var _banner:SpesItAds_WideBanner;
		private var _bottomBar:BottomBar;
		
		private var _loader:Loader;
		
		public function WideBanner()
		{
			_banner = new SpesItAds_WideBanner();
			addChild(_banner);
			
			_bottomBar = new BottomBar(_banner.width);
			_bottomBar.SpesItAds_InfoLinkBtn.x = int(_banner.width - 172);
			_bottomBar.tfInfo.x = int((_banner.width - _bottomBar.tfInfo.width) * .5);
			_bottomBar.y = /*_banner.height*/100;
			addChild(_bottomBar);
			
			_loader = new Loader();
			
			if (stage) bannerInit(null);
			else addEventListener(Event.ADDED_TO_STAGE, bannerInit);
		}
		
		private function loadNextBanner():void
		{
			_refreshInterval = setTimeout(loadBanner, AdsConfig.REFRESH_INTERVAL * 1000);
		}
		
		public function stopRefresh():void
		{
			clearInterval(_refreshInterval);
		}
		
		private function bannerInit(event:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, bannerInit);
			
			stage.addEventListener(Event.RESIZE, stage_resizeHandler);
			stage_resizeHandler();
			
			_banner.mcInnerBanner.addChild(_loader);
			_banner.mcInnerBanner.mouseChildren = false;
			_banner.mcInnerBanner.mouseEnabled = false;
			
			_banner.btLink.addEventListener(MouseEvent.CLICK, banner_clickHandler);
			
			_urlRequest = new URLRequest(AdsConfig.BANNER_URL);
			_urlLoader = new URLLoader();
			
			_userData = <response/>;
			
			_userData.@set = "2";
			_userData.@session = AdsConfig.session;
			_userData.@pid = AdsConfig.PID;
			
			_urlRequest.contentType = "text/xml";
			_urlRequest.data = '<?xml version="1.0" encoding="utf-8"?>' + _userData.toXMLString();
			_urlRequest.method =  URLRequestMethod.POST;
			
			_urlLoader.addEventListener(Event.COMPLETE, data_loadCompleteHandler);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			
			loadBanner();
		}
		
		private function loadBanner():void
		{
			_urlLoader.load(_urlRequest);
		}
		
		private function data_loadCompleteHandler(event:Event):void
		{
			var bannerData:XML = new XML(event.target.data);
			
			_bannerURL = bannerData.@url.toString();
			_bannerLink = bannerData.@link.toString();
			
			_loader.load( new URLRequest(_bannerURL) );
			_loader.contentLoaderInfo.addEventListener(Event.INIT, banner_loadCompleteHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, banner_loadProgressHandler);
		}
		
		private function banner_loadErrorHandler(e:IOErrorEvent):void 
		{
			_loader.contentLoaderInfo.removeEventListener(Event.INIT, banner_loadCompleteHandler);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, banner_loadProgressHandler);
			
			loadNextBanner();
		}
		
		private function banner_loadProgressHandler(event:ProgressEvent):void
		{
			var percent:uint = Math.round( 100 * (event.bytesLoaded / event.bytesTotal) );
			_banner.mcInnerBanner.txtLoader.text = "Загружено " + String(percent) + "%";
		}
		
		private function banner_loadCompleteHandler(event:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.INIT, banner_loadCompleteHandler);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, banner_loadProgressHandler);
			
			loadNextBanner();
		}
		
		private function banner_clickHandler(event:MouseEvent):void
		{
			event.stopImmediatePropagation();
			
			navigateToURL( new URLRequest(_bannerLink), "_blank" );
		}
		
		private function stage_resizeHandler(event:Event = null):void
		{
			this.graphics.clear();
			this.graphics.beginFill(0xFFFFFF, 1);
			this.graphics.drawRect(0, 0, stage.stageWidth, height);
			this.graphics.endFill();
			
			_banner.x = int((stage.stageWidth - 570) * .5);
			//_bottomBar.SpesItAds_InfoLinkBtn.x = int(stage.stageWidth - 172);
			//_bottomBar.tfInfo.x = int((stage.stageWidth - _bottomBar.tfInfo.width) * .5);
			_bottomBar.x = _banner.x;
			
			this.y = int((stage.stageHeight - height));
		}
		
		override public function get height():Number { return /*_banner.height*/100 + /*_bottomBar.height*/18; }
	}
}